<?php
class Image {
	
	public function __construct() {
		
	}
	
	public function uploadFile($source_file, $thumb_width, $hotel_id)
	{
		//$posts = new Posthotel();
		
		//print_r($source_file['file_upload']);
		
		while(list($key,$value) = each($source_file['file_upload']['name']))
		{
		    		    
		    $filename = $hotel_id. "_" . $value;				
			$thumb_img_name = $hotel_id . "_" . $value;
			
			$filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
		    $thumb_img_name=str_replace(" ","_",$thumb_img_name);// Add _ inplace of blank space in file name, you can remove this line
			$add = "images/hotel/" . $filename;   // upload directory path is set
			$thumbs = "images/hotel/thumbs/" . $thumb_img_name;
			
			if(copy($source_file['file_upload']['tmp_name'][$key], $add))
			{
				chmod($add,0777);
				$this->createThumbs('images/hotel/', 'images/hotel/thumbs/', $thumb_width);					
				//$posts->addImages($hotel_id, $filename, $thumb_img_name);
			} else {
			   // $posts->addImages($hotel_id, 'noimage.jpg', 'noimage.jpg');
			    //break;		
			}					
		}	
	}
	
		public function uploadFileEdit($source_file, $cat_id, $target_path, $thumb_width, $hotel_id)
	{
		/*if(isset($source_file['name']))
		{
			$source_file['name'] = $source_file['name'];
		} else {
			$source_file['name'] = 'noimage.jpg';
		}*/
		$userid = $_SESSION['userid'];
		$posts = new Posthotel();
		
		while(list($key,$value) = each($source_file['name']))
		{
		    $max_post_id = $hotel_id;		    
		    $filename = $max_post_id['max_post_id'] . "_" . $userid . "_" . $value;				
			$thumb_img_name = $max_post_id['max_post_id'] . "_" . $userid . "_" . $value;
			
			$filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
		    $thumb_img_name=str_replace(" ","_",$thumb_img_name);// Add _ inplace of blank space in file name, you can remove this line
			$add = "images/hotel/" . $filename;   // upload directory path is set
			$thumbs = "images/hotel/thumbs/" . $thumb_img_name;
			
			if(copy($source_file['tmp_name'][$key], $add))
			{
				chmod($add,0777);
				$this->createThumbs('images/hotel/', 'images/hotel/thumbs/', 130);					
				$posts->EditImages($hotel_id, $filename, $thumb_img_name);
			} else {
			    $posts->EditImages($hotel_id, 'noimage.jpg', 'noimage.jpg');
			    break;		
			}					
		}	
	}

	function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
	{
	  // open the directory
	  $dir = opendir( $pathToImages );
	
	  // loop through it, looking for any/all JPG files:
	  while (false !== ($fname = readdir( $dir ))) {
	    // parse path for the extension
	    $info = pathinfo($pathToImages . $fname);
	    // continue only if this is a JPEG image
	    if ( strtolower($info['extension']) == 'jpg' )
	    {
	      //echo "Creating thumbnail for {$fname} <br />";
	
	      // load image and get image size
	      $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
	      $width = imagesx( $img );
	      $height = imagesy( $img );
	
	      // calculate thumbnail size
	      $new_width = $thumbWidth;
	      //$new_height = floor( $height * ( $thumbWidth / $width ) );
	      $new_height = 100;
	
	      // create a new temporary image
	      $tmp_img = imagecreatetruecolor( $new_width, $new_height );
	
	      // copy and resize old image into new image
	      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
	
	      // save thumbnail into a file
	      imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
	    }
	  }
	  // close the directory
	  closedir( $dir );
	}
	
	
}
?>